package com.doctor.controller.front;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.doctor.entity.Doctor;
import com.doctor.lucene.SearcherKit;
import com.doctor.service.DoctorService;
import com.doctor.utils.StrKit;

@Controller
public class SearchController {
	
	@Resource
	private DoctorService doctorService;
	
	@Resource
	private SearcherKit searcherKit;

	@RequestMapping("/search.html")
	public String index(
			@RequestParam String keyword,
			@RequestParam(required = false,defaultValue="1") Integer p,
			ModelMap map 
			){
		if(StrKit.isBlank(keyword)){
			return "redirect:/";
		}
		map.put("keyWord", keyword);
		Page<Doctor> page = searcherKit.search(p, 10, keyword);
		map.put("page", page);
		map.put("totalnum", page.getTotalElements());
		return "front/search/index";
	}	
	
	@RequestMapping("/doctordetail/{id}")
    public String detail(@PathVariable Long id, ModelMap map) {
        if (id != null) {
            Doctor Doctor = doctorService.findById(id);
            map.put("doctor", Doctor);
        }
        return "front/search/doctor";
    }
}
